import { SUCCESS_CODE } from '@/utils/request';
import type { RequestData } from '@ant-design/pro-table';
import type { AddBatchTaskBean, BatchTaskItem } from './data.d';
import { request } from 'umi';
import { message } from 'antd';

/**
 * 获取批量导入列表
 * @param params
 */
export async function queryTaskList(params: {
  name?: string,
  task_type?: number,
  task_status?: number,
  page?: number,
  pageSize?: number,
}): Promise<RequestData<BatchTaskItem>> {
  const res = await request('/batch/task', {
    params: {
      page: 1,
      pageSize: 10,
      ...params,
    },
  });
  if (!res || res.code !== SUCCESS_CODE) {
    message.error('获取失败');
    return { success: false, total: 0, data: [] };
  }
  return {
    success: true,
    total: res.data.total,
    data: res.data.list,
  };
}

/**
 * 导出批量操作模板
 * @param data 
 */
export async function exportBatchTask(data: any) {
  return await request('/batch/export/template', {
    method: 'POST',
    responseType: `blob`,
    data: {
      type: data.type,
      file_path: data.file_path
    }
  });
}

/**
 * 新增批量导入任务
 * @param data 
 * @returns 
 */
export async function importBatchTask(data: AddBatchTaskBean): Promise<RequestData<BatchTaskItem>> {
  const res = await request('/batch/task/create', {
    method: 'POST',
    data: data
  });

  return { code: res.code, data: res.data, success: true, message: res.message };
}

/**
 * 执行数据导入任务
 * @param id 
 * @returns 
 */
export async function taskImport(id: string): Promise<RequestData<BatchTaskItem>> {
  const res = await request(`/batch/task/${id}/import`, {
    method: 'POST',
  });

  return { code: res.code, data: res.data, success: true, message: res.message };
}
